<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 用户模型
 * @author Calvin<hongbinfu@163.com>
 */
class User_model extends MY_Model {

	function __construct()
	{
		parent::__construct();
	}

	//判断注册用户名是否存在
	public function check_exists($condition=array()){
		return $this->db->select("id")->get_where(self::TB_USER, $condition,1)->row_array();
	}
	//获取用户信息
	public function get_user_info($condition=array(),$field="id"){
		return $this->db->select($field)->get_where(self::TB_USER, $condition,1)->row_array();
	}
	//通过uniqid找到用户信息
	public function get_user_info_by_uniqid($uniqid,$field="id")
	{
		if(empty($uniqid))
		{
			return null;
		}
		$where['uniqid']=$uniqid;
		return $this->get_user_info($where,$field);
	}
	//通过email找到用户信息
	public function get_user_info_by_email($email,$field="id")
	{
		if(empty($email))
		{
			return null;
		}
		$where['email']=$email;
		return $this->get_user_info($where,$field);
	}
	//通过email找到用户信息
	public function get_user_info_by_id($uid,$field="id")
	{
		if(empty($uid))
		{
			return null;
		}
		$where['id']=$uid;
		return $this->get_user_info($where,$field);
	}
	//写进用户表
	public function insert($data){
		return $this->db->insert(self::TB_USER, $data);
	}
	//获取最后插入id
	public function get_insert_id(){
		return $this->db->insert_id();
	}
	//更新用户表
	public function update($id, $data){
		if(empty($id) || empty($data))
		{
			return null;
		}
		return $this->db->where('id', $id)->update(self::TB_USER, $data);
	}
	public function update_user_info($where,$data)
	{
		if(empty($where) || empty($data))
		{
			return null;
		}
		return $this->db->where($where)->update(self::TB_USER, $data);
	}

	public function delete_user($uid)
	{
		return $this->db->where('id', $uid)->delete(self::TB_USER);
	}

	/**
	 * 自增或自减用户表数字字段
	 * @param $id
	 * @param string $field 字段
	 * @param int $offset 加多少或者减多少
	 * @return null
	 */
	public function update_offset($id,$field="used_space",$offset=1)
	{
		if(empty($id) || empty($field) || !is_numeric($offset) || $offset==0)
		{
			return false;
		}
		$this->db->set($field,($offset>0?$field."+".$offset:"IF(`{$field}`{$offset}<0,0,`{$field}`{$offset})"),false);
		return $this->db->where('id', $id)->update(self::TB_USER);
		//$this->db->set();
	}
	public function update_by_uniqid($uniqid,$data)
	{
		if(empty($uniqid) || empty($data))
		{
			return false;
		}
		return $this->db->where('uniqid', $uniqid)->update(self::TB_USER, $data);
	}

	//查询注册次数表ip
	public function query_register_ip($ip){
		return $this->db->select('num, lasttime')->get_where(self::TB_CHECK_LOG, array('type'=>1,'ip' => $ip),1)->row_array();
	}
	//获取注册记录信息
	public function get_register_log_info($where,$field="id,num,lasttime")
	{
		if(empty($where))
		{
			return null;
		}
		$where['type'] = 1;
		return $this->db->select($field)->get_where(self::TB_CHECK_LOG, $where,1)->row_array();
	}
	//记录注册次数
	public function insert_register_log($data){
		if(empty($data))
		{
			return null;
		}
		$data['type'] = 1;
		return $this->db->insert(self::TB_CHECK_LOG, $data);
	}

	//更新注册次数
	public function update_register_log($where, $data){
		if(empty($where))
		{
			return null;
		}
		$where['type'] = 1;
		return $this->db->where($where)->update(self::TB_CHECK_LOG, $data);
	}

	//删除注册记录
	public function delete_register_log($where){
		if(empty($where))
		{
			return null;
		}
		$where['type'] = 1;
		return $this->db->where($where)->delete(self::TB_CHECK_LOG);
	}

	public function fast_delete_register_log($dev_md5="")
	{
		if(!empty($dev_md5))
		{
			$where['device_hash'] = Common::time33($dev_md5);
			$where['device_md5']=$dev_md5;
		}
		else
		{
			$ip = $this->input->ip_address();
			$where['ip_hash'] = Common::time33($ip);
			$where['ip']=$ip;
		}
		$where['type'] = 1;
		return $this->delete_register_log($where);
	}


	//获取登录错误记录信息
	public function get_login_error_log_info($where,$field="id,num,lasttime")
	{
		if(empty($where))
		{
			return null;
		}
		$where['type'] = 2;
		return $this->db->select($field)->get_where(self::TB_CHECK_LOG, $where,1)->row_array();
	}
	//新建登录错误记录
	public function insert_login_error_log($data){
		if(empty($data))
		{
			return null;
		}
		$data['type'] = 2;
		return $this->db->insert(self::TB_CHECK_LOG, $data);
	}
	//修改登录错误记录
	public function update_login_error_log($where, $data){
		if(empty($where))
		{
			return null;
		}
		$where['type'] = 2;
		return $this->db->where($where)->update(self::TB_CHECK_LOG, $data);
	}
	//删除登录错误记录
	public function delete_login_error_log($where)
	{
		if(empty($where))
		{
			return null;
		}
		$where['type'] = 2;
		return $this->db->where($where)->delete(self::TB_CHECK_LOG);
	}
	public function fast_delete_login_error_log($dev_md5="")
	{
		if(!empty($dev_md5))
		{
			$where['device_hash'] = Common::time33($dev_md5);
			$where['device_md5']=$dev_md5;
		}
		else
		{
			$ip = $this->input->ip_address();
			$where['ip_hash'] = Common::time33($ip);
			$where['ip']=$ip;
		}
		$where['type'] = 2;
		return $this->delete_login_error_log($where);
	}

	/**
	 * 获取认证信息
	 * @param $condition
	 * @param string $field
	 * @return mixed
	 */
	public function get_verify_info($condition,$field="id")
	{
		return $this->db->select($field)->get_where(self::TB_VERIFY, $condition,1)->row_array();
	}
	public function update_verify_info($where,$data){
		return $this->db->where($where)->update(self::TB_VERIFY, $data);
	}
	public function update_verify_info_by_id($id,$data){
		if(empty($id) || empty($data))
		{
			return null;
		}
		$where['id']=$id;
		return $this->db->where($where)->update(self::TB_VERIFY, $data);
	}
	public function create_verify_info($data)
	{
		if(empty($data))
		{
			return null;
		}
		if(!isset($data['session_id'])){
			$data['session_id'] = $this->session->userdata("session_id");
		}
		return $this->db->insert(self::TB_VERIFY, $data);
	}
	public function delete_verify_info($where,$string_where="")
	{
		if(!empty($string_where))
		{
			$this->db->where($string_where);
		}
		return $this->db->where($where)->delete(self::TB_VERIFY);
	}

	/**
	 * 添加登录记录
	 * @param $data
	 */
	public function insert_login_log($data)
	{
		return $this->db->insert(self::TB_LOGIN_LOG, $data);
	}

	public function update_login_log_by_id($id,$data)
	{
		if(empty($id) || empty($data))
		{
			return null;
		}
		$where['id']=$id;
		return $this->db->where($where)->update(self::TB_LOGIN_LOG, $data);
	}


	//获取真实的空间
	public function get_real_space()
	{

	}


	//获取授权用户的授权信息
	public function get_auth_user($openid,$from,$key="openid"){
		if(empty($openid) || empty($from))
		{
			return null;
		}
		$where[$key]=$openid;
		$where['from']=$from;
		return $this->db->get_where(self::TB_OAUTH_USER,$where,1)->row_array();
	}
	public function get_auth_info($where){
		if(empty($where))
		{
			return null;
		}
		return $this->db->get_where(self::TB_OAUTH_USER,$where,1)->row_array();
	}
	public function get_auth_info_by_phone($phone,$from,$user_fields="",$oauth_fields="*"){
		if(empty($phone))
		{
			return null;
		}
		$fields=array();
		if(!empty($user_fields) && !is_array($user_fields)){
			$user_fields = explode(",",$user_fields);
		}
		if(!empty($user_fields) && is_array($user_fields)){
			foreach($user_fields as $v){
				$fields[]="u.".$v;
			}
		}
		if(!empty($oauth_fields) && !is_array($oauth_fields)){
			$oauth_fields = explode(",",$oauth_fields);
		}
		if(!empty($oauth_fields) && is_array($oauth_fields)){
			foreach($oauth_fields as $v){
				$fields[]="a.".$v;
			}
		}
		$where = array(
			"u.mobile"=>$phone,
			"a.from"=>$from
		);
		return $this->db
				->select($fields)
				->from(self::TB_USER." u")
				->join(self::TB_OAUTH_USER." a","a.uid=u.id","LEFT")
				->where($where)
				->get('',1)
				->row_array();
	}

	public function add_auth_user($data){
		if(empty($data))
		{
			return 0;
		}
		return $this->db->insert(self::TB_OAUTH_USER, $data);
	}

	public function update_auth_user($id,$data){
		if(empty($id) || empty($data))
		{
			return null;
		}
		$where['id']=$id;
		return $this->db->where($where)->update(self::TB_OAUTH_USER, $data);
	}

	/**
	 * 续期验证码
	 * @param $where
	 * @return mixed
	 */
	public function renew_verify_code($where) {
		$data = array(
			'time' => time()
		);
		return $this->db->where($where)->update(self::TB_VERIFY, $data);
	}
}
